/**
 * main javascript file.
 *
 * @author Zhang Jinlong <466028373@qq.com>
 * @link http://www.ijianzhi.cn
 * @copyright Copyright &copy; 2013 DianJin Soft
 */

/**
 * 初始化
 */
$(function(){
    $('#site-nav-warpper .user-info li:last a').css('border-right', 0);
});


/**
 * 弹出模态窗口
 */
function openPopWindow(url, title, width, height, refresh)
{
    var url = $.trim(url);
    var title = $.trim(title) ? $.trim(title) : 'Information';
    var width = parseInt(width) > 0 ? parseInt(width) : 900;
    var height = parseInt(height) > 0 ? parseInt(height) : 600;
    var refresh = parseInt(refresh) ? parseInt(refresh) : 0;

    if (!url)
    {
        return;
    }

    var option = {
        'title': title,
        'width': width,
        'height': height,
        'lock': true,
        'fixed': true,
        'background': '#000',
        'opacity': .2,
        'close': function()
        {
            if (refresh == 1)
            {
                window.location.reload();
            }
        }
    };

    $.dialog.open(url, option);
}

/**
 * 模拟ALERT函数
 * @param msg
 */
function doAlert(msg)
{
    $.dialog({
        'title': 'Alert',
        'content': msg,
        'icon': 'warning',
        'ok': true,
        'lock': true,
        'fixed': true,
        'opacity': .6
    });
}

/**
 * 模拟CONFIRM函数
 */
function doConfirm(msg, operationOk, operationCancel)
{
    var config = {
        'title': 'Confirm',
        'content': msg,
        'icon': 'question',
        'ok': true,
        'cancel': typeof(operationCancel) == 'function' ? operationCancel : true,
        'lock': true,
        'fixed': true,
        'opacity': .6
    };

    if (typeof(operationOk) === 'function')
    {
        config.ok = operationOk;
    }
    else if (typeof(operationOk) === 'string')
    {
        config.ok = function()
        {
            window.location.href = operationOk;
        };
    }

    $.dialog(config);

    return false;
}

/**
 * 图片上传
 */
function imageUploadHandler(id, type, picture)
{
    var originObject = $('#' + id + '');
    var loading = originObject.parent().next();
    var preview = originObject.parent().parent().next();
    var topParent = originObject.parent().parent().parent();

    var uploader = new qq.FileUploader({
        element: originObject[0],
        action: IMAGE_UPLOAD_TARGE,
        allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp'],
        params: {
            type: type
        },
        onSubmit: function(id, fileName)
        {
            loading.css('display', 'block');

            return true;
        },
        onComplete: function(id, fileName, responseJSON)
        {
            loading.hide();

            if (responseJSON.error != undefined)
            {
                return false;
            }

            originObject.parent().parent().hide();

            var imageHtml = '<img src="' + responseJSON.preview + '" /><a href="">' + delete_str + '</a>';

            preview.append(imageHtml).show();
            preview.find('a').bind('click', imageUploadDelete);

            topParent.find('input.picture_hidden').val(responseJSON.preview);
            topParent.find('input.logo_type').val('2');

            return true;
        },
        showMessage: function(message)
        {
            doAlert(message);
        }
    });

    if (picture != undefined && picture)
    {
        topParent.find('.upload_image_tool').hide();

        var html = '<img src="' + getUploadPictureUrl(picture, 160) + '" /><a href="" onclick="imageUploadHide(this); return false;">' + delete_str + '</a>';

        topParent.find('.upload_image_preview').html(html).show();
        topParent.find('.picture_hidden').val(picture);
    }

    return uploader;
}